package com.im.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.im.common.constant.Constant;
import com.im.common.enums.ResultEnum;
import com.im.common.exception.BusinessException;
import com.im.common.result.JsonPage;
import com.im.common.result.JsonResult;
import com.im.project.entity.Book;
import com.im.project.entity.dto.BaseQueryDto;
import com.im.project.entity.vo.MessageVo;
import com.im.project.mapper.BookMapper;
import com.im.project.rabbitmq.RabbitmqProducer;
import com.im.project.webSocket.ProcessFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 测试 控制层
 *
 * @author yunZhouJiaDi
 */
@Slf4j
@RestController
@RequestMapping("/test")
@Api(tags = "TestController", description = "测试相关接口")
public class TestController {

    @GetMapping("/success")
    @ApiOperation("success")
    public JsonResult success() {
        log.info("测试。。。。");
        return JsonResult.success("success");
    }

    @GetMapping("/failed")
    @ApiOperation("failed")
    public JsonResult failed() {
        throw new BusinessException(ResultEnum.FAILED.getCode(), ResultEnum.FAILED.getMessage());
//        return JsonResult.failed("failed");
    }

    @GetMapping("/error")
    @ApiOperation("error")
    public JsonResult error() {
        return JsonResult.error(ResultEnum.FAILED.getCode(), "error");
    }

    @Resource
    BookMapper bookMapper;

    @GetMapping("/list")
    @ApiOperation("分页查询测试")
    public JsonResult<JsonPage<Book>> queryList(BaseQueryDto baseQueryDto) {
        Page page = bookMapper.selectPage(new Page(baseQueryDto.getPageNum(), baseQueryDto.getPageSize()), new QueryWrapper<Book>());
        return JsonResult.success(JsonPage.restPage(page));
    }

    @PostMapping("/sendPrivate")
    @ApiOperation("发送私聊消息")
    public JsonResult sendPrivate() {
        ProcessFactory.process("你好，这是私聊消息", "U094101404");
        return JsonResult.success(true);
    }

    @Resource
    RedisTemplate redisTemplate;

    @PostMapping("/sendCluster")
    @ApiOperation("发送群聊消息")
    public JsonResult sendCluster() {
        List<String> contactIdList = redisTemplate.opsForList().range(Constant.RedisConstant.CLUSTER_USER + "G082706005", 0, -1);
        contactIdList.forEach(contactId -> {
            ProcessFactory.process("你好，这是群聊消息", contactId);
        });
        return JsonResult.success(true);
    }

    @Resource
    RabbitmqProducer rabbitmqProducer;

    @PostMapping("/sendMessageByMq")
    @ApiOperation("发送消息")
    public JsonResult sendMessageByMq() {
        rabbitmqProducer.sendMessageProducer(Constant.RabbitmqConstant.ROUTING_KEY + Constant.RabbitmqConstant.SEND_MESSAGE,
                new MessageVo<>().
                        setCommandCode(100).
                        setCommandName("mq转发的消息").
                        setContactId("U094101404").
                        setData("你好，mq转发的消息")
        );
        return JsonResult.success(true);
    }

}
